package com.journal.book.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.journal.common.annotation.Log;
import com.journal.common.core.controller.BaseController;
import com.journal.common.core.domain.AjaxResult;
import com.journal.common.core.domain.R;
import com.journal.common.enums.BusinessType;
import com.journal.common.utils.ip.IpUtils;
import com.journal.common.utils.ip.AddressUtils;
import com.journal.book.domain.JournalLocation;
import com.journal.book.service.IJournalLocationService;
import com.journal.common.utils.poi.ExcelUtil;
import com.journal.common.core.page.TableDataInfo;

/**
 * 期刊定位记录Controller
 *
 * @author mobin
 * @date 2025-01-XX
 */
@RestController
@RequestMapping("/book/journalLocation")
public class JournalLocationController extends BaseController
{
    @Autowired
    private IJournalLocationService journalLocationService;

    /**
     * 记录期刊定位（小程序调用）
     * 每次定位都新增一条记录
     *
     * @param request HttpServletRequest 用于获取客户端IP
     * @param journalCode 期刊编号或名称
     * @param shelfId 书架ID
     * @param sideId 书架侧面
     * @param level 书架层数
     * @param userIdentifier 用户唯一标识（可选）
     * @param brand 手机品牌（可选）
     * @param model 设备型号（可选）
     * @param osName 操作系统名称（可选）
     * @return 结果
     */
    @PostMapping("/record")
    public R<Object> recordLocation(
        HttpServletRequest request,
        @RequestParam("journalCode") String journalCode,
        @RequestParam("shelfId") String shelfId,
        @RequestParam("sideId") String sideId,
        @RequestParam("level") Long level,
        @RequestParam(value = "userIdentifier", required = false) String userIdentifier,
        @RequestParam(value = "brand", required = false) String brand,
        @RequestParam(value = "model", required = false) String model,
        @RequestParam(value = "osName", required = false) String osName)
    {
        // 参数校验
        if (journalCode == null || journalCode.isEmpty()) {
            return R.fail("期刊编号不能为空");
        }
        if (shelfId == null || shelfId.isEmpty()) {
            return R.fail("书架ID不能为空");
        }
        if (sideId == null || sideId.isEmpty()) {
            return R.fail("书架侧面不能为空");
        }
        if (level == null) {
            return R.fail("书架层数不能为空");
        }

        // 获取客户端IP地址和地理位置（使用优化后的公共方法）
        String ipAddress = IpUtils.getIpAddr(request);
        String location = AddressUtils.getLocationByIP(ipAddress);

        int result = journalLocationService.recordJournalLocation(
            journalCode, shelfId, sideId, level, userIdentifier, 
            ipAddress, location, brand, model, osName);
        
        return result > 0 ? R.ok() : R.fail("记录定位失败");
    }

    /**
     * 查询期刊定位记录列表
     */
    @PreAuthorize("@ss.hasPermi('book:journalLocation:list')")
    @GetMapping("/list")
    public TableDataInfo list(JournalLocation journalLocation)
    {
        startPage();
        List<JournalLocation> list = journalLocationService.selectJournalLocationList(journalLocation);
        return getDataTable(list);
    }

    /**
     * 导出期刊定位记录列表
     */
    @PreAuthorize("@ss.hasPermi('book:journalLocation:export')")
    @Log(title = "期刊定位记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JournalLocation journalLocation)
    {
        List<JournalLocation> list = journalLocationService.selectJournalLocationList(journalLocation);
        ExcelUtil<JournalLocation> util = new ExcelUtil<JournalLocation>(JournalLocation.class);
        util.exportExcel(response, list, "期刊定位记录数据");
    }

    /**
     * 获取期刊定位记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('book:journalLocation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(journalLocationService.selectJournalLocationById(id));
    }

    /**
     * 新增期刊定位记录
     */
    @PreAuthorize("@ss.hasPermi('book:journalLocation:add')")
    @Log(title = "期刊定位记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JournalLocation journalLocation)
    {
        return toAjax(journalLocationService.insertJournalLocation(journalLocation));
    }

    /**
     * 修改期刊定位记录
     */
    @PreAuthorize("@ss.hasPermi('book:journalLocation:edit')")
    @Log(title = "期刊定位记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JournalLocation journalLocation)
    {
        return toAjax(journalLocationService.updateJournalLocation(journalLocation));
    }

    /**
     * 删除期刊定位记录
     */
    @PreAuthorize("@ss.hasPermi('book:journalLocation:remove')")
    @Log(title = "期刊定位记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(journalLocationService.deleteJournalLocationByIds(ids));
    }
}

